import javax.xml.crypto.Data;
import java.io.*;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;


class NetPkg {
    public String ip;
    public String key1;
    public String key2;
    public String key3;
    
    public NetPkg(String ip, String key1, String key2, String key3) {
        this.ip = ip;
        this.key1 = key1;
        this.key2 = key2;
        this.key3 = key3;
    }
};


class NetPkgGen {
    private static String[] key2key3statistic = {
        "UDQ-sIM-YBIU 6100 1",
        "GTOQgqegnrbcEPy 3741 42",
        "MCUNErCqqQqe 1516 96",
        "MCUInCqqQqe 1488 81",
        "MgrQbekgrPIn 1460 103",
        "MCUzbekgrPNEr 1459 103",
        "ezEezEEril 950 1",
        "HCHVPgfHgmQgqe 931 1",
        "DQVTyPUimg 910 1",
        "ezEwio 908 1",
        "HgmoqyVPgQgqegnr 891 1",
        "QSNDMEmQqoeP 888 1",
        "QqoeVPgqQqoeP 865 1",
        "SoorQSNDMEmQqoeP 810 1",
        "UDQ-DLNTC-YBIU 608 26",
        "MCUuOUorblQgqTge 488 117",
        "QSNDQQHgmQgqe 478 1",
        "MgrubnfYifrAVril 485 98",
        "QSNDQQDQVQgqe 267 1",
        "TYQUorTwbzVPgfQgqegnr 48 1",
        "XWGiPkBwbir 25 29",
        "XWGiPkSkczP 25 29",
        "XWGiPkYkczP 25 29",
        "XWGiPkSzP 25 29",
        "XWGiPYzP 25 29",
        "XWGiPkUbtJFE 21 26",
        "XWGiPkYPetrm 21 26",
        "XWGITOqTDXUH 20 26",
        "tJfiPkUbtJqF 20 26",
        "loJPUBUVT 4 1",
        "VPgqezEEril 1 1",
        "TdTHgmQgqe 1 1",
    };
    private static List<String> key2s = new ArrayList<>();
    private static List<Integer> key23Weight = new ArrayList<>();
    private static List<Integer> numKey3 = new ArrayList<>();
    private static int key23WeightMax;

    private static int pkgTypeNum;


    public static void init() {
        pkgTypeNum = Property.pkgTypeNum;
        for (int i = 0; i < key2key3statistic.length; i++) {
            String[] paras = key2key3statistic[i].split(" ");
            key2s.add(paras[0]);
            key23Weight.add(Integer.parseInt(paras[1]));
            numKey3.add(Integer.parseInt(paras[2]));
        }
        for (int i = 1; i < key23Weight.size(); i++) {
            key23Weight.set(i, key23Weight.get(i) + key23Weight.get(i-1));
        }
        key23WeightMax = key23Weight.get(key23Weight.size() - 1);
    }

    public static String int2ip(int num) {
        int octet1 = (num >> 24) & 0xFF;
        int octet2 = (num >> 16) & 0xFF;
        int octet3 = (num >> 8) & 0xFF;
        int octet4 = num & 0xFF;
        return octet1 + "." + octet2 + "." + octet3 + "." + octet4;
    }

    public static String[] parseIPandClass(int i) {
        String ip = int2ip(i);
        int idx = i % 200;
        String key1;
        if(idx < 170) key1 = "EGWQDWX";
        else if(idx < 192) key1 = "DQFEVC";
        else if(idx < 198) key1 = "OSWDWADQS";
        else key1 = "OSDFX";

        return new String[]{ip, key1};
    }

    public static String[] randomIpKey1(int ipMin, int ipMax) {
        if (ipMin == -1 || ipMax == -1) {
            ipMin = 0;
            ipMax = pkgTypeNum;
        }
        int ip = DataGen.randomInt(0, pkgTypeNum);
        return parseIPandClass(ip);
    }

    public static String[] randomKey23() {
        int num = DataGen.randomInt(0, key23WeightMax);
        int i = 0;
        for (i = 0; i < key23Weight.size() - 1; i++) {
            if (key23Weight.get(i) < num) {
                break;
            }
        }
        String key3 = "key3-" + DataGen.randomInt(1, numKey3.get(i));
        return new String[]{key2s.get(i), key3};
    }

    public static NetPkg genOneRandom(int ipMin, int ipMax) {
        String[] ipKey1 = randomIpKey1(ipMin, ipMax);
        String[] key23 = randomKey23();
        return new NetPkg(ipKey1[0], ipKey1[1], key23[0], key23[1]);
    }
};


public class DataGen {

    public static void init() {
        NetPkgGen.init();
    }
    
    public static int randomInt(int min, int max) {
        Random rand = new Random();
        return rand.nextInt(max + 1 - min) + min;
    }

    public static Timestamp generateTime(){
        return Timestamp.from(Instant.now());
    }

    public static Timestamp timestampNow(){
        return Timestamp.from(Instant.now());
    }

    public static boolean randomBool(){
        Random rand = new Random();
        return rand.nextInt(2) == 1;
    }

    public static NetPkg randomNetPkg(int ipMin, int ipMax) {
        return NetPkgGen.genOneRandom(ipMin, ipMax);
    }
}

